/**
 * @Title: MaterialGroupManage.java
 * 
 * @Description:
 * 
 * @Copyright: Copyright (c) 2010
 * 
 * @Company: www.infoservice.com.cn
 * @Date: 2010-7-1
 * 
 * @author yuyong
 * @mail yuyong@infoservice.com.cn
 * @version 1.0
 * @remark
 */
package com.infodms.dms.actions.sysproduct.productmanage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.dao.productmanage.ProductManageDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TmVhclMaterialGroupPO;
import com.infodms.dms.po.TtAsWrModelItemPO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.dms.chana.actions.OSA11;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.mvc.context.ResponseWrapper;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

/**
 * @author yuyong
 * @param <List>
 * 
 */
public class MaterialGroupManage {
	private Logger logger = Logger.getLogger(MaterialGroupManage.class);
	private ActionContext act = ActionContext.getContext();
	RequestWrapper request = act.getRequest();
	ResponseWrapper response = act.getResponse();
	private final ProductManageDao dao = ProductManageDao.getInstance();

	private final String MATERIAL_GROUP_MANAGE_QUERY_URL = "/jsp/sysproduct/productmanage/materialGroupManageQuery.jsp";// 物料组维护查询页面
	private final String MATERIAL_GROUP_MANAGE_ADD_URL = "/jsp/sysproduct/productmanage/materialGroupManageAdd.jsp";// 物料组维护新增页面
	private final String MATERIAL_GROUP_MANAGE_MOD_URL = "/jsp/sysproduct/productmanage/materialGroupManageMod.jsp";// 物料组维护修改页面

	// 物料组维护pre
	public void materialGroupManageQueryPre() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			act.setForword(MATERIAL_GROUP_MANAGE_QUERY_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "物料组维护");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	// 物料组维护查询
	public void materialGroupManageQuery() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			String groupCode = CommonUtils.checkNull(request
					.getParamValue("groupCode"));
			String groupName = CommonUtils.checkNull(request
					.getParamValue("groupName"));
			String parentGroupCode = CommonUtils.checkNull(request
					.getParamValue("parentGroupCode"));
			String status = CommonUtils.checkNull(request
					.getParamValue("status"));
			String companyId = logonUser.getCompanyId().toString();

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("groupCode", groupCode);
			map.put("groupName", groupName);
			map.put("parentGroupCode", parentGroupCode);
			map.put("status", status);
			map.put("companyId", companyId);

			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage"))
					: 1;
			PageResult<Map<String, Object>> ps = dao
					.getMaterialGroupManageQueryList(map, curPage,
							Constant.PAGE_SIZE);
			act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "物料组维护");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	// 物料组维护新增pre
	public void materialGroupManageAddPre() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			Long companyId = logonUser.getCompanyId();
			List<Map<String, Object>> groups1 = dao.getModelGroupList(
					Constant.WR_MODEL_GROUP_TYPE_01, companyId);// 索赔工时车型组
			List<Map<String, Object>> groups2 = dao.getModelGroupList(
					Constant.WR_MODEL_GROUP_TYPE_02, companyId);// 配件车型组

			act.setOutData("groups1", groups1);
			act.setOutData("groups2", groups2);
			act.setForword(MATERIAL_GROUP_MANAGE_ADD_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "物料组维护");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	// 物料组维护新增
	public void materialGroupManageAdd() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			String groupCode = CommonUtils.checkNull(request
					.getParamValue("groupCode"));
			String groupName = CommonUtils.checkNull(request
					.getParamValue("groupName"));
			String parentGroupCode = CommonUtils.checkNull(request
					.getParamValue("parentGroupCode"));
			String status = CommonUtils.checkNull(request
					.getParamValue("status"));
			String ifType=request.getParamValue("ifType");

			// 获得上级物料组
			TmVhclMaterialGroupPO parent = new TmVhclMaterialGroupPO();
			parent.setGroupCode(parentGroupCode);
			parent = dao.getTmVhclMaterialGroupPO(parent);

			// 物料组保存
			TmVhclMaterialGroupPO po = new TmVhclMaterialGroupPO();
			po.setGroupId(new Long(SequenceManager.getSequence("")));
			po.setGroupCode(groupCode);
			po.setGroupName(groupName);
			if (!parentGroupCode.equals("")) {
				po.setGroupLevel(new Integer(
						parent.getGroupLevel().intValue() + 1));
				String treeCode = dao.getMaterialGroupTreeCode(parent
						.getGroupId().toString());
				po.setTreeCode(treeCode);
				po.setParentGroupId(parent.getGroupId());
			} else {
				String treeCode = dao.getMaterialGroupTreeCode("");
				po.setTreeCode(treeCode);
				po.setGroupLevel(new Integer(1));
			}
			if(ifType.equals(Constant.IF_TYPE_YES.toString())){
				po.setForcastFlag(1);
			}else{
				po.setForcastFlag(0);
			}
			po.setStatus(new Integer(status));
			po.setCreateDate(new Date());
			po.setCreateBy(logonUser.getUserId());
			po.setCompanyId(logonUser.getCompanyId());
			po.setIfStatus(new Integer(0));// 接口状态

			dao.insert(po);

			// 判断上级物料组是否是车系
			if (parent != null && parent.getGroupLevel().intValue() == 2) {
				String modelGroup1 = CommonUtils.checkNull(request
						.getParamValue("modelGroup1"));
				String modelGroup2 = CommonUtils.checkNull(request
						.getParamValue("modelGroup2"));

				// 索赔工时车型组车型关系保存
				TtAsWrModelItemPO itemPo = new TtAsWrModelItemPO();
				itemPo.setModelId(po.getGroupId());
				itemPo.setWrgroupId(new Long(modelGroup1));
				itemPo.setCreateDate(new Date());
				itemPo.setCreateBy(logonUser.getUserId());
				dao.insert(itemPo);

				// 配件车型组车型关系保存
				itemPo = new TtAsWrModelItemPO();
				itemPo.setModelId(po.getGroupId());
				itemPo.setWrgroupId(new Long(modelGroup2));
				itemPo.setCreateDate(new Date());
				itemPo.setCreateBy(logonUser.getUserId());
				dao.insert(itemPo);
			}

			/* 此处需调用接口 */
			OSA11 os = new OSA11();
			os.execute(po);//接口
			act.setOutData("returnValue", 1);

		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "物料组维护");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	// 物料组维护修改pre
	public void materialGroupManageModPre() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			Long companyId = logonUser.getCompanyId();
			String groupId = CommonUtils.checkNull(request
					.getParamValue("groupId"));
			TmVhclMaterialGroupPO po = new TmVhclMaterialGroupPO();
			po.setGroupId(new Long(groupId));
			po = dao.getTmVhclMaterialGroupPO(po);

			TmVhclMaterialGroupPO parent = new TmVhclMaterialGroupPO();
			parent.setGroupId(po.getParentGroupId());
			parent = dao.getTmVhclMaterialGroupPO(parent);

			// 判断物料级别是否是车型
			if (po.getGroupLevel().intValue() == 3) {
				Map<String, Object> group1 = dao.getModelGroup(po.getGroupId(),
						Constant.WR_MODEL_GROUP_TYPE_01);// 索赔工时车型组
				Map<String, Object> group2 = dao.getModelGroup(po.getGroupId(),
						Constant.WR_MODEL_GROUP_TYPE_02);// 配件车型组
				act.setOutData("group1", group1);
				act.setOutData("group2", group2);
			}

			List<Map<String, Object>> groups1 = dao.getModelGroupList(
					Constant.WR_MODEL_GROUP_TYPE_01, companyId);// 索赔工时车型组
			List<Map<String, Object>> groups2 = dao.getModelGroupList(
					Constant.WR_MODEL_GROUP_TYPE_02, companyId);// 配件车型组
			act.setOutData("po", po);
			act.setOutData("parent", parent);
			act.setOutData("groups1", groups1);
			act.setOutData("groups2", groups2);
			act.setForword(MATERIAL_GROUP_MANAGE_MOD_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "物料组维护");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	// 物料组维护修改
	public void materialGroupManageMod() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			String groupId = CommonUtils.checkNull(request
					.getParamValue("groupId"));
			String groupCode = CommonUtils.checkNull(request
					.getParamValue("groupCode"));
			String groupName = CommonUtils.checkNull(request
					.getParamValue("groupName"));
			String parentGroupCode = CommonUtils.checkNull(request
					.getParamValue("parentGroupCode"));
			String status = CommonUtils.checkNull(request
					.getParamValue("status"));
			String ifType=request.getParamValue("ifType");
			TmVhclMaterialGroupPO parent = new TmVhclMaterialGroupPO();
			parent.setGroupCode(parentGroupCode);
			parent = dao.getTmVhclMaterialGroupPO(parent);

			List<Map<String,Object>> groupList = dao.getAllMaterialGroup(groupId);
			if (null != groupList && groupList.size()>0) {
				for (int i = 0; i < groupList.size(); i++) {
					String group_Id = String.valueOf(groupList.get(i).get("GROUP_ID"));
					TmVhclMaterialGroupPO tempPO = new TmVhclMaterialGroupPO();
					tempPO.setGroupId(Long.parseLong(group_Id));
					
					TmVhclMaterialGroupPO valuePO = new TmVhclMaterialGroupPO();
					valuePO.setStatus(Integer.parseInt(status));
					if(ifType.equals(Constant.IF_TYPE_YES.toString())){
						valuePO.setForcastFlag(1);
					}else{
						valuePO.setForcastFlag(0);
					}
					dao.update(tempPO, valuePO);
				}
			}
			
			TmVhclMaterialGroupPO condition = new TmVhclMaterialGroupPO();
			condition.setGroupId(new Long(groupId));

			
			TmVhclMaterialGroupPO value = new TmVhclMaterialGroupPO();
			value.setGroupCode(groupCode);
			value.setGroupName(groupName);
			
			
			if (!parentGroupCode.equals("")) {
				value.setGroupLevel(new Integer(parent.getGroupLevel()
						.intValue() + 1));
				String treeCode = dao.getMaterialGroupTreeCode(parent
						.getGroupId().toString());
				value.setTreeCode(treeCode);
				value.setParentGroupId(parent.getGroupId());
			} else {
				String treeCode = dao.getMaterialGroupTreeCode("");
				value.setTreeCode(treeCode);
				value.setGroupLevel(new Integer(1));
				value.setParentGroupId(null);
			}
			value.setStatus(new Integer(status));
			value.setIfStatus(new Integer(0));// 接口状态
			value.setUpdateDate(new Date());
			value.setUpdateBy(logonUser.getUserId());
			if(ifType.equals(Constant.IF_TYPE_YES.toString())){
				value.setForcastFlag(1);
			}else{
				value.setForcastFlag(0);
			}
			dao.update(condition, value);

			// 删除车型组车型关系
			TtAsWrModelItemPO itemPo = new TtAsWrModelItemPO();
			itemPo.setModelId(new Long(groupId));
			dao.delete(itemPo);

			// 判断上级物料组是否是车系
			if (parent != null && parent.getGroupLevel().intValue() == 2) {
				String modelGroup1 = CommonUtils.checkNull(request
						.getParamValue("modelGroup1"));
				String modelGroup2 = CommonUtils.checkNull(request
						.getParamValue("modelGroup2"));

				// 索赔工时车型组车型关系保存
				itemPo = new TtAsWrModelItemPO();
				itemPo.setModelId(new Long(groupId));
				itemPo.setWrgroupId(new Long(modelGroup1));
				itemPo.setCreateDate(new Date());
				itemPo.setCreateBy(logonUser.getUserId());
				dao.insert(itemPo);

				// 配件车型组车型关系保存
				itemPo = new TtAsWrModelItemPO();
				itemPo.setModelId(new Long(groupId));
				itemPo.setWrgroupId(new Long(modelGroup2));
				itemPo.setCreateDate(new Date());
				itemPo.setCreateBy(logonUser.getUserId());
				dao.insert(itemPo);
			}

			/* 此处需调用接口 */
			OSA11 os = new OSA11();
			os.execute(value);//接口
			act.setOutData("returnValue", 1);

		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.UPDATE_FAILURE_CODE, "物料组维护");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	// 物料组维护查询
	public void getModelGroup() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			Long companyId = logonUser.getCompanyId();
			String groupCode = CommonUtils.checkNull(request
					.getParamValue("groupCode"));

			TmVhclMaterialGroupPO group = new TmVhclMaterialGroupPO();
			group.setGroupCode(groupCode);
			List<PO> list = dao.select(group);
			group = list.size() == 0 ? null : (TmVhclMaterialGroupPO) list
					.get(0);

			// 判断上级物料组是否是车系
			if (group != null && group.getGroupLevel().intValue() == 2) {
				List<Map<String, Object>> groups1 = dao.getModelGroupList(
						Constant.WR_MODEL_GROUP_TYPE_01, companyId);// 索赔工时车型组
				List<Map<String, Object>> groups2 = dao.getModelGroupList(
						Constant.WR_MODEL_GROUP_TYPE_02, companyId);// 配件车型组
				act.setOutData("returnValue", 1);
				act.setOutData("groups1", groups1);
				act.setOutData("groups2", groups2);
			} else {
				act.setOutData("returnValue", 2);
			}

		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "物料组维护");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

}
